//
//  JShopCarTableCell.m
//  HeadIndustry
//
//  Created by jun on 2017/9/4.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JShopCarTableCell.h"

@interface JShopCarTableCell ()
{
    JCellDelBlock delBlock;
    JNumberChangedBlock numberAddBlock;
    JNumberChangedBlock numberCutBlock;
    JCellSelectedBlock cellSelectedBlock;
}

//删除
@property (nonatomic,strong) UIButton *delBtn;
//显示照片
@property (nonatomic,strong) UIImageView *ImageView;
//商品名
@property (nonatomic,strong) YYLabel *nameLabel;
//尺寸
@property (nonatomic,strong) YYLabel *sizeLabel;
//时间
@property (nonatomic,strong) YYLabel *dateLabel;
//价格
@property (nonatomic,strong) YYLabel *priceLabel;
//数量
@property (nonatomic,strong)YYLabel *numberLabel;

@end


@implementation JShopCarTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.opaque = NO;
        self.backgroundColor = [UIColor whiteColor];
        _product = [JProductDetailModel new];
        [self setUpCell];
    }
    return self;
}

- (void)setUpCell {
    //白色背景
    UIView *bgView = [[UIView alloc]init];
    bgView.frame = CGRectMake(10, 10, kWindowW - 20, 100 - 10);
    bgView.backgroundColor = kRGBColor(245, 245, 245);
    bgView.layer.borderColor = kRGBColor(245, 245, 245).CGColor;
    bgView.layer.borderWidth = 1;
    [self addSubview:bgView];
    
    //选中按钮
    UIButton* selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn.center = CGPointMake(20, bgView.height/2.0);
    selectBtn.bounds = CGRectMake(0, 0, 30, 30);
    [selectBtn setImage:[UIImage imageNamed:SelBtnImg_N] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:SelBtnImg_H] forState:UIControlStateSelected];
    [selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:selectBtn];
    self.selectBtn = selectBtn;
    
    //照片背景
    UIView *imageBgView = [[UIView alloc]init];
    imageBgView.frame = CGRectMake(selectBtn.right + 5, 5, bgView.height - 10, bgView.height - 10);
    imageBgView.backgroundColor = kCOLOR_HEX(0xF3F3F3);
    [bgView addSubview:imageBgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgTapGesture:)];
    [imageBgView addGestureRecognizer:tap];
    
    //显示照片
    UIImageView* imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"default_pic_1"];
    imageView.frame = CGRectMake(imageBgView.left, imageBgView.top, imageBgView.width, imageBgView.height);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [bgView addSubview:imageView];
    self.ImageView = imageView;
    
    CGFloat width = bgView.width - imageBgView.right - 30 ;
    
    //商品名
    YYLabel* nameLabel = [[YYLabel alloc]init];
    nameLabel.frame = CGRectMake(imageBgView.right + 10, 10, width, 40);
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.numberOfLines = 0;
    [bgView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    //尺寸
    YYLabel* sizeLabel = [[YYLabel alloc]init];
    sizeLabel.frame = CGRectMake(nameLabel.left, nameLabel.bottom, width, 20);
    sizeLabel.textColor = kRGBColor(150, 150, 150);
    sizeLabel.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:sizeLabel];
    self.sizeLabel = sizeLabel;
    
    //时间
    YYLabel* dateLabel = [[YYLabel alloc]init];
    dateLabel.frame = CGRectMake(nameLabel.left, sizeLabel.bottom , width, 20);
    dateLabel.font = [UIFont systemFontOfSize:12];
    dateLabel.textColor = [UIColor blackColor];
    [bgView addSubview:dateLabel];
    self.dateLabel = dateLabel;
    
    //价格
    YYLabel* priceLabel = [[YYLabel alloc]init];
    priceLabel.frame = CGRectMake(bgView.right - width - 10, sizeLabel.bottom, width, 20);
    priceLabel.font = [UIFont boldSystemFontOfSize:12];
    priceLabel.textColor = [UIColor blackColor];
    priceLabel.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    _editView = [[UIView alloc]initWithFrame:CGRectMake(imageBgView.right + 5, 5, bgView.width - (imageBgView.right + 5), bgView.height)];
    _editView.backgroundColor = kRGBColor(245, 245, 245);
    [bgView addSubview:_editView];
    _editView.hidden = YES;
    
    //删除按钮
    UIButton *delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    delBtn.frame = CGRectMake(_editView.width - 45, 10, 40, 40);
    [delBtn setTitle:@"删除" forState:UIControlStateNormal];
    delBtn.backgroundColor = [UIColor redColor];
    [delBtn addTarget:self action:@selector(delBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_editView addSubview:delBtn];
    
    //数量加按钮
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(_editView.width - 100, delBtn.top, 40, 40);
    addBtn.backgroundColor = kRGBColor(238, 238, 238);
    [addBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_editView addSubview:addBtn];
    
    //数量减按钮
    UIButton *cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cutBtn.frame = CGRectMake(10, addBtn.top, 40, 40);
    cutBtn.backgroundColor = kRGBColor(238, 238, 238);
    [cutBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
    [cutBtn addTarget:self action:@selector(cutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_editView addSubview:cutBtn];
    
    //数量显示
    YYLabel* numberLabel = [[YYLabel alloc]init];
    numberLabel.left = cutBtn.right + 10;
    numberLabel.top = addBtn.top;
    numberLabel.size = CGSizeMake(addBtn.left - cutBtn.right - 20, 40);
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.text = @"1";
    numberLabel.font = [UIFont systemFontOfSize:16];
    numberLabel.backgroundColor = kRGBColor(238, 238, 238);
    [_editView addSubview:numberLabel];
    self.numberLabel = numberLabel;
    
}
#pragma mark - imgTapGesture
- (void)imgTapGesture:(UITapGestureRecognizer *)tap
{
    if (self.product.Id > 0) {
        if (_imgBlock) {
            _imgBlock(self.product);
        }
    }
}


#pragma mark - 重写setter方法
- (void)setDwqNumber:(NSInteger)dwqNumber {
    _dwqNumber = dwqNumber;
    
    self.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)dwqNumber];
    self.priceLabel.text = [NSString stringWithFormat:@"%ld",(long)dwqNumber];
}

- (void)setDwqSelected:(BOOL)dwqSelected {
    _dwqSelected = dwqSelected;
    self.selectBtn.selected = dwqSelected;
}

#pragma mark - 按钮点击方法
- (void)delBtnClick:(UIButton*)button {
    
    if (delBlock) {
        delBlock();
    }
}
- (void)selectBtnClick:(UIButton*)button {
    button.selected = !button.selected;
    
    if (cellSelectedBlock) {
        cellSelectedBlock(button.selected);
    }
}

- (void)addBtnClick:(UIButton*)button {
    
    NSInteger count = [self.numberLabel.text integerValue];
    count++;
    
    if (numberAddBlock) {
        numberAddBlock(count);
    }
}

- (void)cutBtnClick:(UIButton*)button {
    NSInteger count = [self.numberLabel.text integerValue];
    count--;
    if(count <= 0){
        return ;
    }
    
    if (numberCutBlock) {
        numberCutBlock(count);
    }
}

#pragma mark - public method
- (void)reloadDataWithModel:(JShopCarDetailModel*)model {
    self.product.Id = model.ProductId;
    self.product.Name = model.ProductName;
    self.product.VendorId = model.VendorId;
    
    
    self.ImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.ImgUrl]]];
    self.nameLabel.text = model.ProductName;
    self.priceLabel.text = [NSString stringWithFormat:@"X%ld",(long)model.Quantity];
    self.dateLabel.text = model.CustomerEnteredPriceText;
    self.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)model.Quantity];
    
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[model.AttributesXml dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    self.sizeLabel.attributedText = attrStr;
    self.selectBtn.selected = model.isSel;
}


- (void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;

    if (_isEdit) {
        _editView.hidden = NO;
    }
    else
    {
        _editView.hidden = YES;
    }
}

- (void)delCellBtnWithBlock:(JCellDelBlock)block {
    delBlock = block;
}

- (void)numberAddWithBlock:(JNumberChangedBlock)block {
    numberAddBlock = block;
}

- (void)numberCutWithBlock:(JNumberChangedBlock)block {
    numberCutBlock = block;
}

- (void)cellSelectedWithBlock:(JCellSelectedBlock)block {
    cellSelectedBlock = block;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
